简体   繁体   English

如何使用javascript或jquery将参数值设置为“onClick”函数

[英]How to set parameter values to a “onClick” function using javascript or jquery

I am trying to set parameters to onClick event inside a html button using javascript . 我正在尝试使用javascript参数设置为html button内的onClick事件。

This is how html button looks like: 这就是html按钮的样子:

<button type="button" id="upload-button" onclick="uploadFileIGCSE('param1', 'param2', 'param3')" class="btn btn-success" aria-expanded="false">Upload</button>

param1 = should be the value from a html select element with id='year' and name='year' param1 =应该是来自id='year'name='year'html select元素的值

param2 = should be the value from a html select element with id='grade' and name='grade' param2 =应该是来自id='grade'name='grade'html select元素的值

param3 = should be the value from a html select element with id='subject' and name='subject' param3 =应该是来自id='subject'name='subject'html select元素的值

These are the html select options 这些是html select options

<div class="col-md-3">
    <select id="year" name="year" class="form-control">
        <option value="select" disabled="disabled">- Select Year -</option>
        <option value="1">2001</option>
        <option value="2">2002</option>
        <option value="3">2003</option>
    </select>
</div>

<div class="col-md-3">
    <select id="grade" name="grade" class="form-control">
        <option value="select" disabled="disabled">- Select Grade -</option>
        <option value="1">Grade 5</option>
        <option value="2">Grade 6</option>
        <option value="3">Grade 7</option>
    </select>
</div>

<div class="col-md-3">
    <select id="subject" name="subject" class="form-control">
        <option value="select" disabled="disabled">- Select Subject -</option>
        <option value="1">Edexcel</option>
        <option value="2">National</option>
        <option value="3">Other</option>
    </select>
</div>

I have no clue how to set values to param1 , param2 , param3 我不知道如何设置值为param1param2param3

This is what I tried but it feels wrong. 这是我尝试过但感觉不对。

$('#upload-button').val = $('#year').val;

Can someone please help me? 有人可以帮帮我吗?

I need to set values for the parameters (param1, param2, param3) in the function uploadFileIGCSE('param1', 'param2', 'param3') using javascript or jquery 我需要使用javascriptjquery在函数uploadFileIGCSE('param1', 'param2', 'param3')设置参数值(param1,param2,param3)

Thank you 谢谢

I can understand your needs. 我能理解你的需求。 if you really wanted to do this only with HTML element, here is the way. 如果你真的只想用HTML元素这样做,这就是方法。

<button type="button" id="upload-button" 
    onclick="uploadFileIGCSE(
             document.querySelector('#year').value, 
             document.querySelector('#grade').value, 
             document.querySelector('#subject').value)" 
    class="btn btn-success" aria-expanded="false">Upload</button>

Here is the working example 这是工作示例

 function uploadFileIGCSE(val1, val2, val3){ document.querySelector('#log').innerHTML = `${val1}, ${val2}, ${val3}` } 
 <div class="col-md-3"> <select id="year" name="year" class="form-control"> <option value="select" disabled="disabled">- Select Year -</option> <option value="1">2001</option> <option value="2">2002</option> <option value="3">2003</option> </select> </div> <div class="col-md-3"> <select id="grade" name="grade" class="form-control"> <option value="select" disabled="disabled">- Select Grade -</option> <option value="1">Grade 5</option> <option value="2">Grade 6</option> <option value="3">Grade 7</option> </select> </div> <div class="col-md-3"> <select id="subject" name="subject" class="form-control"> <option value="select" disabled="disabled">- Select Subject -</option> <option value="1">Edexcel</option> <option value="2">National</option> <option value="3">Other</option> </select> </div> <button type="button" id="upload-button" onclick="uploadFileIGCSE( document.querySelector('#year').value, document.querySelector('#grade').value, document.querySelector('#subject').value)" class="btn btn-success" aria-expanded="false">Upload</button> <p>log value : (<span id="log"></span>)</p> 

You really shouldn't be using inline event listeners like onclick (for maintainability, security and separation of concerns reasons). 你真的不应该使用像onclick这样的内联事件监听器(出于可维护性,安全性和关注点分离的原因)。

Instead, add the listener using the DOM API's HTMLElement.prototype.addEventListener() : 相反,使用DOM API的HTMLElement.prototype.addEventListener()添加侦听器:

document.getElementById('upload-button').addEventListener('click', function() {
  uploadFileIGCSE(year.value, grade.value, subject.value);
})

 function uploadFileIGCSE(x,y,z) { console.log(x,y,z); } document.getElementById('upload-button').addEventListener('click', function() { uploadFileIGCSE(year.value, grade.value, subject.value); }) 
 <div class="col-md-3"> <select id="year" name="year" class="form-control"> <option value="select" disabled="disabled">- Select Year -</option> <option value="1">2001</option> <option value="2">2002</option> <option value="3">2003</option> </select> </div> <div class="col-md-3"> <select id="grade" name="grade" class="form-control"> <option value="select" disabled="disabled">- Select Grade -</option> <option value="1">Grade 5</option> <option value="2">Grade 6</option> <option value="3">Grade 7</option> </select> </div> <div class="col-md-3"> <select id="subject" name="subject" class="form-control"> <option value="select" disabled="disabled">- Select Subject -</option> <option value="1">Edexcel</option> <option value="2">National</option> <option value="3">Other</option> </select> </div> <button type="button" id="upload-button">Upload</button> 

try this 尝试这个

<button type="button" id="upload-button" onclick="getOption()" class="btn btn-success"
    aria-expanded="false">Upload</button>

function getOption() {
    var year = document.getElementById("year");
    var year_option = year.options[year.selectedIndex].value;

    var grade = document.getElementById("grade");
    var grade_option = grade.options[grade.selectedIndex].value;

    var subject = document.getElementById("subject");
    var subject_option = subject.options[subject.selectedIndex].value;

    uploadFileIGCSE(year_option, grade_option, subject_option)
}

The click event can simply run an anonymous function. click事件可以简单地运行匿名函数。 Its only input will be the default event object supplied by the DOM engine. 它唯一的输入将是DOM引擎提供的默认event对象。 That function can then gather the values from the various elements you're interested in, and make a further call to the uploadFileIGCSE function, passing those values as parameters. 然后,该函数可以从您感兴趣的各种元素中收集值,并进一步调用uploadFileIGCSE函数,将这些值作为参数传递。

Here's a demo. 这是一个演示。 Note that I've used an unobtrusive event handler using addEventListener() in the JS code, rather than "onclick" within the HTML. 请注意,我用一个不显眼的使用事件处理程序的addEventListener()的JS代码,而不是“onclick”事件在HTML中。 This is generally considered to be better practice, as it makes the code easier to read, maintain and debug, and helps to separate the functionality from the presentation. 这通常被认为是更好的实践,因为它使代码更易于阅读,维护和调试,并有助于将功能与表示分开。

NB you could use jQuery for any/all of this, but equally it's not really necessary, it doesn't really add much value in this situation. 注意,您可以将jQuery用于任何/所有这些,但同样不是必需的,它在这种情况下并没有真正增加很多价值。

 var button = document.getElementById("upload-button"); button.addEventListener("click", function(event) { var year = document.getElementById("year").value; var grade = document.getElementById("grade").value; var subject = document.getElementById("subject").value; uploadFileIGCSE(year, grade, subject); }); function uploadFileIGCSE(year, grade, subject) { console.log(year, grade, subject); } 
 <button type="button" id="upload-button" onclick="uploadFileIGCSE('param1', 'param2', 'param3')" class="btn btn-success" aria-expanded="false">Upload</button> <div class="col-md-3"> <select id="year" name="year" class="form-control"> <option value="select" disabled="disabled">- Select Year -</option> <option value="1">2001</option> <option value="2">2002</option> <option value="3">2003</option> </select> </div> <div class="col-md-3"> <select id="grade" name="grade" class="form-control"> <option value="select" disabled="disabled">- Select Grade -</option> <option value="1">Grade 5</option> <option value="2">Grade 6</option> <option value="3">Grade 7</option> </select> </div> <div class="col-md-3"> <select id="subject" name="subject" class="form-control"> <option value="select" disabled="disabled">- Select Subject -</option> <option value="1">Edexcel</option> <option value="2">National</option> <option value="3">Other</option> </select> </div> 

$('#upload-button').click(function(e) {
    var year = $('#year').val();
    var grade = $('#grade').val();
    var subject = $('#subject').val();

    uploadFileIGCSE(year, grade, subject);
})

You could try something like this. 你可以尝试这样的事情。

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

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