如何从一个选择选项发送到另一个选项?

时间:2012-10-03 16:39:19

标签: javascript jquery

(可能)这里简单的jQuery问题。我正在使用jQuery UI Dialog,我正在进行确认。基本上,当用户选择某个选项(与其他情况相结合)时,我希望能够将它们发送回他们的最后一个选择,或者如果我不能这样做,则为默认选项。

该选项也没有“选定”属性,这可能是我的首选示例。有没有办法做到这一点?

这是js:

alert('hey')
if (document.frm.notNull.checked == true) {
    // This is where it ends!
    if ($('input[name=valueTextField]').length > 1) {
        $('#dialog p').empty();
        $('#dialog p').html("You're going to lose your stuff if you do this. You sure you want to?");
        $('#dialog').dialog({
            autoOpen: true,
            width: 600,
            modal: true,
            buttons: {
                "<spring:message code='dialogBox.cancelConfirmation.YES'/>": function() {
                    $(this).dialog("close");
                    $("#bottom").css('visibility', 'visible');
                    $('.edit-new').css('visibility', 'hidden');
                    $('.edit-trash').css('visibility', 'hidden');
                    // if table has more than one row, delete the rest.
                    deleteExtraRows('valueTable');
                    return true;

                },
                "<spring:message code='dialogBox.cancelConfirmation.NO'/>": function() {
                    $(this).dialog("close");

                    // Need to revert the user back to where they came from here.
                    $('#control[value=1]').attr('selected', true);
                    // return false;
                }
            }
        });
    } else {
        $("#bottom").css('visibility', 'visible');
        $('.edit-new').css('visibility', 'hidden');
        $('.edit-trash').css('visibility', 'hidden');
        // if table has more than one row, delete the rest.
        deleteExtraRows('valueTable');
        return true;

     }
 }​

这是HTML(动态呈现):

<div class="lbl">
    <label>Control</label>
</div>
<select style="margin:0 0 0 8px; left:15px; position:relative;" name="control"
id="control" onChange="checkTableVisibility();">
    <option value=1>Check Box</option>
    <option value=0>Dropdown Menu</option>
    <option value=3>Radio Button</option>
    <option value=2 selected="selected">Text Box</option>
</select>

2 个答案:

答案 0 :(得分:0)

我想我很清楚你的问题 我的回答是:  使用变量来存储你可以这样做的所选值:

$("yourSelect").change(function(){
 selected = $(this).val();
});

然后您可以将点击值重置为重置按钮,例如

$("#reset").click(function(){
  $("yourSelect").val(selected);
 });

答案 1 :(得分:0)

以下是示例代码。希望这可以帮到你

##### Jquery Part #####
<script>
$(document).ready(function(){
 var dropdownControl =$("#control");
 var selectedValue = dropdownControl.val(); 
 dropdownControl.change(function(){
  var dropItem = $(this);
  $('#dialog').dialog({
  autoOpen: true,
      width: 600,
      modal: true,
      buttons: {
          'Ok': function(){
                  selectedValue = dropItem.val();
                  $(this).dialog("close");
               },
               'Cancel' : function(){
                     dropdownControl.val(selectedValue);
                     $(this).dialog("close");
               } 
              }
  });
  });
});
</script>


 #### HTML #####
<body>
 <div>
    <select style="margin:0 0 0 8px; left:15px; position:relative;" name="control" id="control">
     <option value=1>Check Box</option>
     <option value=0>Dropdown Menu</option>
     <option value=3>Radio Button</option>
     <option value=2 selected="selected">Text Box</option>
    </select>
 </div>
 <div id="dialog" style="display:none;">
   Do you want to change value ?
 </div>
</body>
 

我希望你能放更多代码。我添加了一些可能对您有帮助的代码。请仔细阅读我的更改和评论。我将整个代码包装到jQuery ready函数中。如果有效,请告诉我。

 $(document).ready(function(){
  var dropdownControl =$("#control");
  var previousValue = dropdownControl.val(); 
  /// Please add above line of code on your jQuery onload section. So that previous value get set, for the first time, whenever dom gets load. so that you can use it for later cases
alert('hey')
if (document.frm.notNull.checked == true) {
  // This is where it ends!
  if ($('input[name=valueTextField]').length > 1) {
      $('#dialog p').empty();
      $('#dialog p').html("You're going to lose your stuff if you do this. You sure    you want to?");
    $('#dialog').dialog({
        autoOpen: true,
        width: 600,
        modal: true,
        buttons: {
            "<spring:message code='dialogBox.cancelConfirmation.YES'/>": function() {
                $(this).dialog("close");
                $("#bottom").css('visibility', 'visible');
                $('.edit-new').css('visibility', 'hidden');
                $('.edit-trash').css('visibility', 'hidden');
                previousValue = dropdownControl.val();
                // please reset previousValue with the new one that you approved to change so that next time when dropdown changes it value it could use it
                // if table has more than one row, delete the rest.
                deleteExtraRows('valueTable');
                return true;

            },
            "<spring:message code='dialogBox.cancelConfirmation.NO'/>": function() {
                $(this).dialog("close");

                // Need to revert the user back to where they came from here.
                $('#control[value=1]').attr('selected', true);
                 dropdownControl.val(previousValue);
                // Please replace dropdown value with the old one if you want to revert changes.
                // return false;
            }
        }
    });
} else {
    $("#bottom").css('visibility', 'visible');
    $('.edit-new').css('visibility', 'hidden');
    $('.edit-trash').css('visibility', 'hidden');
    // if table has more than one row, delete the rest.
    deleteExtraRows('valueTable');
    return true;

 }
}​ 
});