简体   繁体   English

使用jquery对文本框进行集中处理

[英]confitional focus on textbox using jquery

I have two checkboxes like this: 我有两个这样的复选框:

<input id="isGenericPassword" name="isGenericPassword"
       onclick="IsGenericOrUnique(this.id);" type="checkbox" value="true" />
<input name="isGenericPassword" type="hidden" value="false" />
<input checked="checked" id="isUniquePassword" name="isUniquePassword"
       onclick="IsGenericOrUnique(this.id);" type="checkbox" value="true" />
<input name="isUniquePassword" type="hidden" value="false" />
<input id="UniquePassword" name="UniquePassword" style="display:none"
       type="text" value="" />

in document.ready, I have 在document.ready中,我有

 $('#isUniquePassword').is(':checked')
     ? $("#UniquePassword").show()
     : $("#UniquePassword").hide();

so that related textbox is visible if checkbox is checked. 因此,如果选中了复选框,则相关的文本框是可见的。

I am hiding the textbox UniquePassword on click of isUniquePassword checkbox 我在单击isUniquePassword复选框时隐藏了文本框UniquePassword

$("#isUniquePassword").click(function () {
    $("#UniquePassword").toggle(this.checked);
    if ($("#isUniquePassword").is(':checked')) {
        // unique password gets focus only if related checkbox is checked
        $("#UniquePassword").focus();
    }
});

and show dialogue when user tries to go away without typing anything 并在用户不输入任何内容而试图离开时显示对话

 $("#UniquePassword").blur(function () {
     if ($("#isUniquePassword").is(':checked') &&
         $("#UniquePassword").val() == '')
     {
         $("#dialog-RecipientEmail-error").dialog("open");                        
     }
 });

and on Ok button click of that dialogue, setting focus again to that textbox so that value is entered 然后在“确定”按钮上单击该对话框,再次将焦点设置到该文本框,以便输入值

  $("#dialog-RecipientEmail-error").dialog({
      autoOpen: false,
      resizeable: false,
      width: 300,
      modal: true,
      buttons: {
          "OK": function () {
              $(this).dialog("close");
              $("#UniquePassword").focus();
          }
      }
  });

No I want that if isGenericPassword is checked or isUniquePassword is unchecked then I should get that error dialogue 不,我希望如果isGenericPassword被选中或isUniquePassword被取消选中,那么我应该得到该错误对话框

$("#UniquePassword").focus(); $(“#UniquePassword”)。focus(); will not focus the field rather it will trigger the actions which are bound with focus event. 不会聚焦该字段,而是将触发与焦点事件绑定的操作。 To focus a field, you need to get the handle of the HTML element 要聚焦一个字段,您需要获取HTML元素的句柄

$("#UniquePassword").get(0).focus();

That should work. 那应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM