如何设置选项:在多个选择中选择

时间:2009-06-21 09:12:48

标签: jquery select input option

经过一番努力,我得到了以下代码来解决我的问题。但是,它仍然不会将选定内容添加到所选选项中。您可以在下面找到我原来的问题。 改进了代码,但它仍然不起作用。

var $index = $('.drop').index(this)
// find drop index...
var $indexofmonth = $index + 1;
//alert($indexofmonth); // this works
var $indeximg = event.dragTarget.title;
var $indeximgnum = $indeximg.replace(/[a-zA-Z]/g,"");
//alert($indeximgnum); // this gives only number 1,2,3,4 etc. Use this in option:eq(2)
var $featurenumber = "Feature" + $indexofmonth;
//alert($featurenumber); //this works
var $eqnum = 'option:eq('+$indeximgnum + ')';
//alert($eqnum); //working

//and set up like this $('select[name=Feature1] option:eq(3)').attr('selected', 'selected' );
// find the drop select
$sel = $("select[name='"+ $featurenumber +"']");
//the above html() give an array
$('$sel $eqnum').attr('selected', 'selected' );
//alert ('$sel $eqnum'); //this does not work

+++++++++++++++++++++++++++++

这是我原来的问题。

我有以下HTML和jQuery。 HTML是CMS的输出。

我使用http://threedubmedia.com/demo/drop/拖放,因为它们总共6k。 jQuery UI核心本身就超过了100k。

我想将一张图片放到一个月。有20多个图像,当然还有12个月。为此我使用了拖放。

我只使用了四张图片和四个月用于演示目的。

我整理了如何拖放部分。但我很困惑如何创建一个函数,以便从正确的月份选择一个正确的图像。

例如,如果我在1月份放下海豚图像,我想在1月份选择海豚选择选项。

我认为这是我需要做的,但不是100%肯定。

总体而言:掉落区域的标题必须等于select的FeatureValue,例如januar是FeatureValue1

步骤1:找到丢弃区域/月的索引号1,2,3 ..并将其指定为var monthindex

步骤2:然后将var index添加到FeatureValue var featurenumber

步骤3:如果tis与select中的FeatureValue(索引号)相同,请执行以下操作

步骤4:找到已删除图像的标题var = droppedimg

步骤5:添加:如果lostimg =已删除的img的标题,则选择为选择选项值,lostimg

我赞美你的帮助。提前谢谢。

HTML

<div id="calendarimg">
<div id="status"></div>
<div id="calendartable">

<div id="jan" class="drop" Title="januar" >&nbsp;</div>
<div id="feb" class="drop" Title="febrar" >&nbsp;</div>
<div id="mar" class="drop" Title="mars" >&nbsp;</div>
<div id="apr" class="drop" Title="april">&nbsp;</div>
<div id="mai" class="drop" Title="mai">&nbsp;</div>
<div id="jun" class="drop" Title="juni">&nbsp;</div>
<div id="jul" class="drop" Title="juli">&nbsp;</div>
<div id="aug" class="drop" Title="august">&nbsp;</div>
<div id="sep" class="drop" Title="september">&nbsp;</div>
<div id="oct" class="drop" Title="october">&nbsp;</div>
<div id="nov" class="drop" Title="november">&nbsp;</div>
<div id="dec" class="drop" Title="december">&nbsp;</div>

</div>


<div id="nodrop">

<div class="drag" title="Angel" id="angel1"></div>
<div class="drag" title="Dolphin" id="dolphin2"></div>
<div class="drag" title="Fantail" id="fantail3"></div>
<div class="drag" title="Hawk" id="hawk4"></div>


</div>
<!-- End of printimgs --> 

</div>

<div id="featureright2">

<form method="post" action="http://www.mywebsite.com/shopaddtocart.asp">
<table cellspacing="1" cellpadding="2" border="0"><tr><td class="al">
<p class="al"><strong>januar</strong></p>
<select size="3" name="FeatureValue1">
    <option value="" selected="selected">velg</option>
    <option value="97">Angel</option>
    <option value="98">Dolphin</option>
    <option value="99">Fantail</option>
    <option value="89">Hawk</option>

</select>
<input type="hidden" name="Feature1" value="88">
<p class="al"><strong>februar</strong></p>
<select size="3" name="FeatureValue2">
    <option value="" selected="selected">velg</option>
    <option value="109">Angel</option>
    <option value="110">Dolphin</option>
    <option value="111">Fantail</option>
    <option value="101">Hawk</option>

</select>
<input type="hidden" name="Feature2" value="100">
<p class="al"><strong>mars</strong></p>
<select size="3" name="FeatureValue3">
    <option value="" selected="selected">velg</option>
    <option value="112">Angel</option>
    <option value="121">Dolphin</option>
    <option value="122">Fantail</option>
    <option value="123">Hawk</option>

</select>
<input type="hidden" name="Feature3" value="120">
<p class="al"><strong>april</strong></p>
<select size="3" name="FeatureValue4">
    <option value="" selected="selected">velg</option>
    <option value="124">Angel</option>
    <option value="133">Dolphin</option>
    <option value="134">Fantail</option>
    <option value="135">Hawk</option>

</select>
<input type="hidden" name="Feature4" value="132">


</td></tr><tr><td class="ac">

<input class="txtfield" type="text" size="2" maxlength="3" name="quantity" value="1" />
</td><td class="ac vm"><input class="imgbtn" src="images/vpnav_buy_norw.gif" type="image"></td></tr>
<input type="hidden" name="productid" value="81" />
</table></form>
</div>

的jQuery

$(".drag")
    .bind( "dragstart", function( event ){
            // ref the "dragged" element, make a copy
            var $drag = $( this ), $proxy = $drag.clone();
            // modify the "dragged" source element
            $drag.addClass("outline");
            // insert and return the "proxy" element                
            return $proxy.appendTo( document.body ).addClass("ghost");
            })
    .bind( "drag", function( event ){
            // update the "proxy" element position
            $( event.dragProxy ).css({
                    left: event.offsetX,
                    top: event.offsetY
                    });
            })
    .bind( "dragend", function( event ){
            // remove the "proxy" element
            $( event.dragProxy ).fadeOut( "normal", function(){
                    $( this ).remove();
                    });
            // if there is no drop AND the target was previously dropped
            if ( !event.dropTarget && $(this).parent().is(".drop") ){
                    // output details of the action
                    $('#status').empty().append('<div>Removed <b>'+ this.title +'</b> from <b>'+ this.parentNode.title +'</b></div>');
                    // put it in it's original div...
                    $('#nodrop').append( this );
                    }
            // restore to a normal state
            $( this ).removeClass("outline");      

            });
$(".drop")
    .bind( "dropstart", function( event ){
            // don't drop in itself
            if ( this == event.dragTarget.parentNode ) return false;
            // activate the "drop" target element
            $( this ).addClass("active");
            })
    .bind( "drop", function( event ){
            // if there was a drop, move some data...
            $( this ).append( event.dragTarget );
            // output details of the action...
            $('#status').empty().append('<div>Dropped <b>'+ event.dragTarget.title +'</b> into <b>'+ this.title +'</b></div>');             


            })
    .bind( "dropend", function( event ){
            // deactivate the "drop" target element
            $( this ).removeClass("active");
            });

2 个答案:

答案 0 :(得分:2)

这应该这样做:

你的堕落......

.bind( "dropend", function( event ){
  // deactivate the "drop" target element
  $( this ).removeClass("active");

  // added here:
  $("select[name='FeatureValue" + ($.inArray(this, $('#calendartable').children() )+1) + " Multiple']").children('option').each(function(){ if ( $(this).html() === $(event.dragTarget).attr('title') ) $(this).attr('selected', 'selected') });

});

答案 1 :(得分:0)

你的问题我认为归结为你的最终选择。 '$sel $eqNum'不是有效的jQuery选择器。 JavaScript不会在字符串中进行变量替换。此外,在所有变量名称上使用$会让人感到非常困惑。我尝试仅将$用于jQuery对象的变量。稍微重写您的变量声明以反映这一点。在代码块的底部附近,您实际上为select($sel)检索jQuery对象,您可以使用该对象上的.find()方法根据eqnum查找选项

var index = $('.drop').index(this);
var indexofmonth = index + 1;

var indeximg = event.dragTarget.title;
var indeximgnum = indeximg.replace(/[a-zA-Z]/g,"");

var featurenumber = "Feature" + indexofmonth;
var eqnum = 'option:eq('+indeximgnum+')';

// find the drop select
var $sel = $("select[name="+featurenumber+"]");

// find the option using eqnum from before
$sel.find(eqnum).attr('selected', 'selected' );

// or do it all in one brush stroke:
$('select[name='+featurenumber+'] option:eq('+indeximgnum+')').attr('selected','selected');

$函数从选择器字符串返回一个jQuery对象,您传递了'$sel $eqNum'

相关问题