0

(可能)简单的 jQuery 问题在这里。我正在使用 jQuery UI 对话框,并且正在确认。基本上,当用户选择某个选项(结合其他情况)时,我希望能够将他们发送回他们上次选择的选项,或者如果我不能这样做,则返回默认选项。

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

这是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>
4

2 回答 2

0

我想我很理解你的问题我的回答是:使用变量来存储选择的值你可以这样做 id 像这样:

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

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

$("#reset").click(function(){
  $("yourSelect").val(selected);
 });
于 2012-10-03T16:50:53.297 回答
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 就绪函数中。请让我知道它是否有效。

 $(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;

 }
}​ 
});
于 2012-10-03T17:53:58.180 回答