简体   繁体   English

选择标记php生成的代码需要javascript交互

[英]Select tag php generated code requires javascript interaction

Currently I am working on a piece of javascript to copy a link plus the option the user has selected within the select tag. 目前,我正在使用一段JavaScript来复制链接以及用户在select标签中选择的选项。

The variable I assign in the javascript remains undefined. 我在javascript中分配的变量仍未定义。 Is there a way to make this code work? 有没有办法使此代码有效? I do not wish to use the ID attribute with a loop unless this can be applied into my PHP script (in which I echo the select tags, because I'm showing database results, whenever a result is shown, it will also show a select tag). 我不希望将ID属性与循环一起使用,除非可以将其应用到我的PHP脚本中(在该脚本中,我回显select标记,因为我正在显示数据库结果,每当显示结果时,它也会显示一个select标签)。 This link shows a simple example of what I made so far: http://jsfiddle.net/b31au78v/ 该链接显示了到目前为止我所做的一个简单示例: http : //jsfiddle.net/b31au78v/

Here is my code: 这是我的代码:

<table>
    <tr>
        <th>Name</th>
        <th>Gender</th>
    </tr>
    <tr>
        <td>Hank</td>
        <td>
            <select onChange="getValue()" class="ddlEval">
                <option>Select an option...</option>
                <option val="gender.php?=male">Male</option>
                <option val="gender.php?=female">Female</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>Jeff</td>
        <td>
            <select onChange="getValue()" class="ddleval">
                <option>Select an option...</option>
                <option val="gender.php?=male">Male</option>
                <option val="gender.php?=female">Female</option>
            </select>
        </td>
    </tr>
</table>

<script>
    function getValue() {
    optVal = this.value;
    if(optVal != "Select an option...")
    {
    window.prompt("This is the link that's going to be coppied: ", "localhost:8080/" + optVal);
    }
}
</script>

I guess I need to replace "this" with something else, but it is only to show what I actually want to happen. 我想我需要用其他东西代替“ this”,但这只是为了显示我实际想要发生的事情。

Consider the following approach: 考虑以下方法:

var optVal = selectTag.options[selectTag.selectedIndex].value;

In your case ( jsFiddle ): 在您的情况下( jsFiddle ):

<script>
    function getValue(s) {
    optVal = s.options[s.selectedIndex].value;
    if(optVal != "Select an option...")
    {
    window.prompt("This is: ", "localhost:8080/gender.php?=" + optVal);
    }
}
</script>

While changing the onChange event code: 更改onChange事件代码时:

        <select onChange="getValue(this)"...

Please notice that you should also pass the id/name of the member, so when someone changes the first member's gender it won't conflict with the second one. 请注意,您还应该传递成员的ID /名称,因此当某人更改第一个成员的性别时,它不会与第二个成员发生冲突。

There are two issues here: 这里有两个问题:

There's no attribute val it should be value : 没有属性val它应该是value

<option value="none">Select an option …</option>
<option value="gender.php?=male">Male</option>

Note : If you don't have an attribute value the value of the element will be the text of the option -element selected instead. 注意 :如果没有属性value ,则元素的值将是option -element option文本


this in your function has a different scope, so it's not aware of a property called this.value . 函数中的this具有不同的作用域,因此它不知道名为this.value的属性。 You can however pass the element into the function, like: 但是,您可以将元素传递给函数,例如:

<select onchange="getValue(this);"></select>

Then you can get its value like: 然后,您可以得到其值,例如:

function getValue(e) {
    var optVal = e.value;
    if ('none' != optVal) {
        window.prompt("This is the link that's going to be coppied: ", "localhost:8080/" + optVal);
    }
}

Demo 演示版

Try before buy 购买前尝试

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

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