【问题标题】:Change a selected value based on selection根据选择更改选定的值
【发布时间】:2015-08-01 02:14:01
【问题描述】:

这是我想要做的: 如果用户选择“添加新”选项,我想添加一个文本框并获取用户输入并将其保存到我的帖子中。这部分正在工作。 while 循环提供了我的数据库中已经存在的其他选项。如果用户选择它们,我会将选择的结果保存到我的帖子中。但是,我永远无法让它发挥作用。我尝试了不同的方法来处理 if 语句下的内容。我尝试设置不保存的 id 值。我试图做我为 else 语句所做的事情,但是 javascript 不解析引号内的变量,这意味着我不能将变量传递给值。我对此很陌生。谁能帮帮我吗。非常感谢。

<label for="bbp_extra_field2">Your Idea</label><br>
<form>
     <select id="bbp_extra_field2"  onchange="addtextbox()">
        <option value ="new">Add New</option>;
        <?php 
            while ( bbp_replies() ) : bbp_the_reply();
                 $reply_id  =   bbp_get_reply_id();
                 $ideas     =   get_post_meta( $reply_id, 'bbp_extra_field2', true);
                echo "<option value='$ideas' selected>".$ideas."</option>";
            endwhile; ?>
    </select>
</form>
<div id="newidea"></div>
<script>
    var e = document.getElementById("bbp_extra_field2");
    var strUser = e.options[e.selectedIndex].value;
    function addtextbox() {

        if (strUser != "new") {
            return
        }     
        else {
            document.getElementById("newidea").innerHTML = "<input type='text' name='bbp_extra_field2'>";
        }        
    }
</script>

以下代码适用于我的数据库中已有的选项,并且可以保存结果。但是,不允许我添加新值。非常感谢。

echo '<label for="bbp_extra_field2">Idea</label><br>';
echo '<select name="bbp_extra_field2" id="field2">';
echo '<option value ="new">Add New</option>';
while ( bbp_replies() ) : bbp_the_reply();
    $reply_id = bbp_get_reply_id();
    $ideas = get_post_meta( $reply_id, 'bbp_extra_field2', true);
    echo "<option value='$ideas' selected>".$ideas."</option>";
endwhile;
echo "</select>";

【问题讨论】:

    标签: javascript php select save onchange


    【解决方案1】:

    我不确定我明白你想要做什么,但我认为你正在尝试这样做:

    <label for="bbp_extra_field2">Your Idea</label><br>
    <form>
         <select id="bbp_extra_field2"  onchange="addtextbox()">
            <option value ="new">Add New</option>
            <?php 
                while ( bbp_replies() ) : bbp_the_reply();
                    $reply_id = bbp_get_reply_id();
                     $ideas = get_post_meta( $reply_id, 'bbp_extra_field2', true);
                    echo "<option value='$ideas' selected>".$ideas."</option>";
                endwhile; ?>
        </select>
        <div id="newidea"></div>
    </form>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>    
    <script>
    $("#bbp_extra_field2").change(function() {
            var Selector    =   $(this).val();
            $("#newidea").html('<input type="text" value="'+Selector+'" name="bbp_extra_field2">');
        });
    </script>
    

    jsFiddle 演示:

    https://jsfiddle.net/muu982tx/

    【讨论】:

    • 非常感谢。绝对向我展示了一种很好的新方式来看待这个问题。我会试一试,但我认为它让我更接近我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2023-03-07
    相关资源
    最近更新 更多