简体   繁体   中英

jQuery get name attribute of any select input using this

UPDATE

How is what I am asking, different from this example from jQuery

<script>
$( "div" ).click(function() {
var color = $( this ).css( "background-color" );
$( "#result" ).html( "That div is <span style='color:" +
color + ";'>" + color + "</span>." );
});
</script>

I am building this parameter based search engine that when a person selects a value from a specific select box, that value is added to a text input which can be modified directly eg. typing.

I have that part, what I'm trying to solve is how to remove repeats eg. if someone decides to change the value that they had selected for one select input that they had already selected, the value should replace the old value.

I am trying to solve this by building a variable / instance of a list which ads on as more select fields are selected eg. have value.

What I tried to apply is this

So I have a select input like this -side note- I realize that onchange and onclick may be redundant if I can accomplish all in one but it's nice to know I have the option to use more than one.

<select name="name of select" id="id of select" onchange="changeFunc(value);" onclick="check();">

So I have this to create a variable / instance to append select values to

function addParameters(){
// build selected options list

function currentOptionsSelected(){
this.value = "";
}

var cOS = new currentOptionsSelected();

function updateCOS(fnc){

}

}

That is the plan once I get this to work

function check(){
var nameOfSelectBox = $(this).attr("name");
$( "select" ).click(function() {
var nameOfSelectBox = $( this ).attr("name");
var nameOfSelectBox = $('[name=" "]');
alert(nameOfSelectBox);
});
var nameOfSelectBox = $("select").attr("name");
alert(nameOfSelectBox);
updateCOS(fnc);
}

As you can see I was trying different methods.

So, I check with str.match() if the select name already has been used eg. selected and then I update the value

If there is a better way, suggestions are welcome.

Thank you

Her tis

$( "select" ).click(function() {
var nameOfSelectBox = $( this ).attr("name");
alert(nameOfSelectBox);
});

This alerts the person of what select box they clicked, then from here you create a variable of concatenated strings and append this to that string and then string.match() for the name of the select box in order to remove it and place a new one if the person changes the value. That's the idea anyway. Will have to see if I can do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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