jquery <选择>问题

发布于 2024-10-27 16:43:03 字数 736 浏览 2 评论 0 原文

<select name="securityq1" class="selectInput">
    <option value="">Please select</option>
    <option value="What is your matric no?">What is your matric no?</option> 
    <option value="What is the name of your lover?" >What is the name of your lover?</option>
    <option value="What is your profession?" >What is your profession?</option>
</select>

大家好,这是我的安全问题,当我想做更新功能时,我想从数据库中提取用户之前输入的问题。如果问题很少,我可以愚蠢地做

但是安全问题可能多达15个问题,这样是不是很繁琐?由于我使用 jQuery 作为 UI,我怎样才能快速使用 jQuery?感谢您的帮助

<select name="securityq1" class="selectInput">
    <option value="">Please select</option>
    <option value="What is your matric no?">What is your matric no?</option> 
    <option value="What is the name of your lover?" >What is the name of your lover?</option>
    <option value="What is your profession?" >What is your profession?</option>
</select>

Hi all, this is my security question, when I want to do the update function, I want to draw from the database the question users have previously entered. If questions are little, I can do stupidly <option value="What is your matric no?" <?php if($database['securityq1']=="What is your matric no") echo "selected"; ?>>...</option>

But the security questions may up to 15 questions, do this way is very tedious? As I am using jQuery for UI , how can I make a quick way using jQuery? Thanks for help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪痕残 2024-11-03 16:43:03

您必须在服务器端以某种方式确定所选值。我建议您将选项保留在数组或其他集合中并循环它们:

$options = array("What is your matric no?", ...); 
foreach($options as $option) {
   echo "<option value=\"$option\"";
   if($database['securityq1'] === $option) {
      echo ' selected';
   }
   echo ">$option</option>";
}

您可以将例程包装在函数中并更轻松地处理所有类似的情况。

如果你坚持使用 jQuery,你可以这样做,

$('select[name=securityq1] option[value="'+ selectedSecurityQuestion +'"]').attr('selected','selected');

其中 selectedSecurityQuestion 在某处定义。 查看实际效果。

但是,如果关闭 javascript,此解决方案将不起作用。这是否是一个问题由目标受众决定。

You have to determine the selected value somehow on the server-side. I'd suggest that you keep the options in an array or some other collection and loop over them:

$options = array("What is your matric no?", ...); 
foreach($options as $option) {
   echo "<option value=\"$option\"";
   if($database['securityq1'] === $option) {
      echo ' selected';
   }
   echo ">$option</option>";
}

You can wrap the routine in a function and handle all similar situations easier.

If you insist using jQuery you can do something like this

$('select[name=securityq1] option[value="'+ selectedSecurityQuestion +'"]').attr('selected','selected');

Where selectedSecurityQuestion is defined somewhere. See it in action.

However,this solution won't work if javascript is turned off. Whether that is a problem is determined by the target audience.

捶死心动 2024-11-03 16:43:03

尝试一次..

<script>

    $("select").change(function () {
          var str = "";
          $("select option:selected").each(function () {
                str += $(this).text() + " ";
              });
          $("div").text(str);
        })
        .trigger('change');
</script>

try with this once ..

<script>

    $("select").change(function () {
          var str = "";
          $("select option:selected").each(function () {
                str += $(this).text() + " ";
              });
          $("div").text(str);
        })
        .trigger('change');
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文