简体   繁体   English

当我传递参数时,从动态添加的html按钮调用javascript函数失败

[英]call to a javascript function from a dynamically added html button fails when I pass params

First I add a menu with selectable items 首先,我添加一个带有可选项目的菜单

</style>
<div id='form'><select id="mySelect" multiple="multiple" onclick="myFunction()">
  <option value="1">Please select a Form type</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
  <option value="4">Fourth</option>
</select></div>

If I chosse item number two there is some javascript code that runs and add html code dinamically to the page. 如果我选择第二项,则将运行一些javascript代码,并将html代码动态添加到页面中。 As you can see there is a submit button in the html code, that calls another javascript function, which is setType(); 如您所见,html代码中有一个提交按钮,该按钮调用另一个javascript函数setType();。

<script type="text/javascript"> 
var foo = document.getElementById('mySelect');
var param1;
var param2;
var param3;
function myFunction(){
    if (foo)
    {
       if (foo.selectedIndex != null )
       {
           if(foo.selectedIndex>'0')
            {

                if(foo.selectedIndex == '1'){
                    //cargamos formulario 1

                    var newElement ="<div id='div1'>Advertiser ID<input type='text' name='Avertiser' value='Advertiser'><br><input type='submit' value='Submit advertiser' onClick='setType('Advertiser')';></div><div id='div2'>Offer: <input type='text' name='Offer' value='Offer'><br><input type='submit' value='Submit Offer'></div><div id='div3'>Date: <input type='text' name='Date' value='Date'><br><input type='submit' value='Submit Date'></div>";
                    var bodyElement = document.body;
                    bodyElement.innerHTML = newElement + bodyElement.innerHTML;
                    document.body.insertAdjacentHTML("" );
                //  ("Advertiser Id: <input type='text' name='AD' value='AD'><br><input type='submit' value='SubmitAD'>Offer: <input type='text' name='Offer' value='Offer'><br><input type='submit' value='SubmitOffer'>Date: <input type='text' name='Date' value='Date'><br><input type='submit' value='Date'>");                    
                }
                if(foo.selectedIndex == '2'){
                    //cargamos formulario 2

                }
                if(foo.selectedIndex == '3'){
                    //cargamos formulario 3

                }
           }
       } 
    }
}

My problem is I want to pass the Advertiser Value as a param into this javascript function, to store it in a global variable, and when the three submit buttons params are in their variable, then make an ajax call to another file. 我的问题是我想将Advertiser Value作为参数传递给此javascript函数,以将其存储在全局变量中,并且当三个提交按钮的参数位于其变量中时,再对另一个文件进行ajax调用。 My problem is when I call the javascript function with the param, it doesn't run. 我的问题是,当我使用参数调用javascript函数时,它无法运行。 Without params works. 没有参数的作品。 I don't understand the problem. 我不明白这个问题。

function setType(id){
    alert('ok');
}

This 'setType('Advertiser')' won't work, try 'setType(\\"Advertiser\\")' 'setType('Advertiser')'不起作用,请尝试'setType(\\"Advertiser\\")'

Here is a working fiddle 这是一个工作的小提琴

check your "" :) 'setType('Advertiser')' is bad code, try 检查您的“” :) 'setType('Advertiser')'是错误代码,请尝试

onClick="setType('Advertiser')"

and in HTML ALWAYS use "" in atributes, not '' !!! 并且在HTML中总是在属性中使用"" ,而不是'' !!!!

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

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