当我选择“其他”时显示文本输入从选择控件

发布于 2024-11-28 16:23:10 字数 1581 浏览 3 评论 0原文

请任何人帮助我适当地显示和隐藏文本框。

Response: 
  <label>
    <select name="call_response" id="call_response">
    <option value="No Answer">No Answer</option>
    <option value="Not Available">Not Available</option>
    <option value="Not In Use">Not In Use</option>
    <option value="On MC, PL">On MC, PL</option>
    <option value="Pending Case">Pending Case</option>
    <option value="Other">Others - To specify</option>
    </select>
  </label><br><div id="hiddendiv">
<input type="text" id="othertextbox" />
</div>

jQuery 代码:

$(function()
{
$("#hiddendiv").hide();
});

$("#call_response").change(function()
{
if($("#call_response").find(":selected").val($(this).val()) == "Other")
{
    $("#hiddendiv").show();
}
});

$("#othertextbox").change(function(){

$("#call_response").val($("#othertextbox").text());});


从 OP 评论更新:

“我尝试使用 javascript 得到了答案。我如何更改为 jquery?请给出 一些想法。”

function setupScript() {
    OtherOnchanged('call_response', 'SPAN_Other', 'Other');
}
function displayNhide(display, status) {
    document.getElementById(display).style.display = status;
}
function OtherOnchanged(onChangedid, spanId, valueToCheck) {
    var value = document.getElementById(onChangedid).value;
    if (value == valueToCheck) {
        displayNhide(spanId, 'block');
    } else {
        displayNhide(spanId, 'none');
    }
}

Please any one help me to show and hide the text box appropriately.

Response: 
  <label>
    <select name="call_response" id="call_response">
    <option value="No Answer">No Answer</option>
    <option value="Not Available">Not Available</option>
    <option value="Not In Use">Not In Use</option>
    <option value="On MC, PL">On MC, PL</option>
    <option value="Pending Case">Pending Case</option>
    <option value="Other">Others - To specify</option>
    </select>
  </label><br><div id="hiddendiv">
<input type="text" id="othertextbox" />
</div>

jQuery code:

$(function()
{
$("#hiddendiv").hide();
});

$("#call_response").change(function()
{
if($("#call_response").find(":selected").val($(this).val()) == "Other")
{
    $("#hiddendiv").show();
}
});

$("#othertextbox").change(function(){

$("#call_response").val($("#othertextbox").text());});


Update from OP comment:

"I try in javascript I got the answer. How I can change to jquery? Please give
some idea."

function setupScript() {
    OtherOnchanged('call_response', 'SPAN_Other', 'Other');
}
function displayNhide(display, status) {
    document.getElementById(display).style.display = status;
}
function OtherOnchanged(onChangedid, spanId, valueToCheck) {
    var value = document.getElementById(onChangedid).value;
    if (value == valueToCheck) {
        displayNhide(spanId, 'block');
    } else {
        displayNhide(spanId, 'none');
    }
}

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

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

发布评论

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

评论(5

呆头 2024-12-05 16:23:10

您不能将

真实值添加一个隐藏字段,如下所示:

<label> Response:
<select name="call_response" id="call_response">
    <option value="No Answer">No Answer</option>
    <option value="Not Available">Not Available</option>
    <option value="Not In Use">Not In Use</option>
    <option value="On MC, PL">On MC, PL</option>
    <option value="Pending Case">Pending Case</option>
    <option value="Other">Others - To specify</option>
</select>
</label><br>

<input type="hidden" id="real_call_response"/>

<div id="hiddendiv">
    <input type="text" id="othertextbox"/>
</div>

然后用这个 jQuery 激活它:

$("#call_response").change ( function () {
    $("#real_call_response").val ( $("#call_response").val () );

    if ( $("#call_response").val ()  ==  "Other") {
        $("#hiddendiv").show().find('input').focus();
    }
    else
        $("#hiddendiv").hide();
} );

$("#othertextbox").bind ("change keypress keyup blur", function () {
    $("#real_call_response").val ( $("#othertextbox").val () );
} );

注意:最初使用 CSS 隐藏 hiddendiv

在 jsFiddle 上查看所有操作。



更新:

新代码已转换到 jQuery 就是:

function setupScript() {
    OtherOnchanged('call_response', 'SPAN_Other', 'Other');
}

function OtherOnchanged (onChangedId, spanId, valueToCheck) {

    if ( $("#" + onChangedId).val() ==  valueToCheck)
        $("#spanId").show ()
    else
        $("#spanId").hide ()
}

You cannot set a <select>s value to something for which there is no option (at least with jQuery).

Add a hidden field for the real value, like so:

<label> Response:
<select name="call_response" id="call_response">
    <option value="No Answer">No Answer</option>
    <option value="Not Available">Not Available</option>
    <option value="Not In Use">Not In Use</option>
    <option value="On MC, PL">On MC, PL</option>
    <option value="Pending Case">Pending Case</option>
    <option value="Other">Others - To specify</option>
</select>
</label><br>

<input type="hidden" id="real_call_response"/>

<div id="hiddendiv">
    <input type="text" id="othertextbox"/>
</div>

Then activate it all with this jQuery:

$("#call_response").change ( function () {
    $("#real_call_response").val ( $("#call_response").val () );

    if ( $("#call_response").val ()  ==  "Other") {
        $("#hiddendiv").show().find('input').focus();
    }
    else
        $("#hiddendiv").hide();
} );

$("#othertextbox").bind ("change keypress keyup blur", function () {
    $("#real_call_response").val ( $("#othertextbox").val () );
} );

Note: Use CSS to hide hiddendiv initially.

See it all in action at jsFiddle.



Update:

That new code, converted to jQuery is just:

function setupScript() {
    OtherOnchanged('call_response', 'SPAN_Other', 'Other');
}

function OtherOnchanged (onChangedId, spanId, valueToCheck) {

    if ( $("#" + onChangedId).val() ==  valueToCheck)
        $("#spanId").show ()
    else
        $("#spanId").hide ()
}
檐上三寸雪 2024-12-05 16:23:10

你的想法是正确的,但有一个小错字。使用以下内容(工作 JSFiddle):

 $(function() {
   $("#hiddendiv").hide();
 });

 $("#call_response").change(function() {
   //EDIT: Just call val(), not val(...)
   if($("#call_response").find(":selected").val() == "Other") {
     $("#hiddendiv").show();
   }
 });

 $("#othertextbox").change(function(){

 $("#call_response").val($("#othertextbox").text());});

You have the right idea, but you have a small typo. Use the following (working JSFiddle):

 $(function() {
   $("#hiddendiv").hide();
 });

 $("#call_response").change(function() {
   //EDIT: Just call val(), not val(...)
   if($("#call_response").find(":selected").val() == "Other") {
     $("#hiddendiv").show();
   }
 });

 $("#othertextbox").change(function(){

 $("#call_response").val($("#othertextbox").text());});
梨涡少年 2024-12-05 16:23:10

您应该将观察者置于文档就绪事件中的元素。否则,如果 dom 没有准备好并且 dom 元素不存在,那么你的观察者将毫无用处。

$(function()
{
$("#hiddendiv").hide();

$("#call_response").change(function()
{
if($("#call_response").find(":selected").val($(this).val()) == "Other")
{
    $("#hiddendiv").show();
}
});

$("#othertextbox").change(function(){

$("#call_response").val($("#othertextbox").text());});

});

You should put your observer to the elements within the document ready event. Otherwise your observer would be useless if the dom is not ready and the dom element is not there.

$(function()
{
$("#hiddendiv").hide();

$("#call_response").change(function()
{
if($("#call_response").find(":selected").val($(this).val()) == "Other")
{
    $("#hiddendiv").show();
}
});

$("#othertextbox").change(function(){

$("#call_response").val($("#othertextbox").text());});

});
与他有关 2024-12-05 16:23:10

您是否尝试过将事件处理程序分配移动到文档就绪处理程序中?否则,他们可能找不到您尝试附加它们的元素,因为它们尚未准备好。

$(function() {
  $("#hiddendiv").hide();

  $("#call_response").change(function() {
    if($("#call_response").find(":selected").val($(this).val()) == "Other") {
      $("#hiddendiv").show();
    }
  });

  $("#othertextbox").change(function(){
    $("#call_response").val($("#othertextbox").text());
  }); 

});

Have you tried moving your event handler assignments inside the document ready handler? Otherwise chances are they're not finding the elements you're trying to attach them to because they're not ready yet.

$(function() {
  $("#hiddendiv").hide();

  $("#call_response").change(function() {
    if($("#call_response").find(":selected").val($(this).val()) == "Other") {
      $("#hiddendiv").show();
    }
  });

  $("#othertextbox").change(function(){
    $("#call_response").val($("#othertextbox").text());
  }); 

});
遗忘曾经 2024-12-05 16:23:10

试试这个

$(function()
{
   $("#hiddendiv").hide();

  $("#call_response").change(function()
  {
    if($(this).val() == "Other")
    {
      $("#hiddendiv").show();
    }
  });

$("#othertextbox").change(function(){

   $("#call_response").val($(this).text());
});

});

Try this

$(function()
{
   $("#hiddendiv").hide();

  $("#call_response").change(function()
  {
    if($(this).val() == "Other")
    {
      $("#hiddendiv").show();
    }
  });

$("#othertextbox").change(function(){

   $("#call_response").val($(this).text());
});

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文