0

我正在使用带有 PHP 的 jQuery 脚本来根据下拉选择有条件地显示/隐藏表单字段。
脚本是:

//Hide the field initially
        $("#hide1").hide();

        //Show the text field only when the third option is chosen - this doesn't
        $('#project-type').change(function() {
                if ($("#project-type").val() == "Value 1") {
                        $("#hide1").show();
                }
                else {
                        $("#hide1").hide();
                }
        });

我希望能够向其中添加一个值数组,if ($("#project-type").val() == "Value 1")因此如果有五个值,我希望某个字段显示何时选择值 1 和 3 而不是其他值。我尝试了一些方法,但它们都导致所有值都显示隐藏字段。
任何帮助表示赞赏。

4

2 回答 2

1

jQuery inArray 应该做的工作;

var val = $("#project-type").val(); // Project type value.

var show = $.inArray(val, ['Value 1', 'Value 2', ...]) > -1 ? 'show' : 'hide'; // If the value is in the array, show value is 'show' otherwise it is 'hide'.

$("#hide1")[show](); // Show or hide $('#hide1').
于 2013-07-27T22:21:22.173 回答
0

有很多选择,一个简单的方法是将您的 if 更改为:

if ($("#project-type").val() == "Value 1" || $("#project-type").val() == "Value 3")

或者:

if (["Value 1", "Value 3"].indexOf($("#project-type").val()) != -1)
于 2013-07-27T21:01:47.930 回答