New to whole this jQuery (and javascript altogether, heh) and so far it's been excellent, but now I'm in a small pickle.
Let's say I have list of forms generated from SQL database and every single one of them has to have unique id, so how I can select the specific item that is to be manipulated (changing values via php).
the $("#submit").click(function()) will trigger every submit buttons on the page, so how I can the #submit to be some random id that I clicked. There might be a smarter way, but I'm new to this so try to bear with me.
thought of passing the unique value with onClick="myfunction(unique_id)", but don't know how it goes with jQuery.
hope this made any sense
$("#submit") won't intercept every submit button click, but only the element with id="submit".
If you want to get the form's id attribute you can use a snippet like this:
$("form").submit(function () {
var selectedFormID = $(this).attr('id');
});
I might not be understanding the question correctly but if each of the submit buttons has a unique id, e.g. <button id="submit_02">Submit</button> then the jQuery would be $("#submit_02").click(function() {}).
Related
I am a bit new to mvc razor and building websites (front and backend). The break down is, I need a button that has a value stored in it be sent to the controller/model. Something similar to html boxtextfor. I have tried giving boxtextfor attributes similar to an input submit button, but it doesn't like that. I have tested the button using javascript and it does have the value within each individual button (# of buttons are dynamic based on previous submit).
I have seen posts like this but I am unsure how to add these to my controller or model so my index page can call it. My model is linked to my index page so I guess I could link these methods in my model.
There's no #Html.Button !
(tried this, but it needs to be linked to my model. A simple button doesn't work.)
Looking for a Html.SubmitButton helper that would accept class attributes in MVC3
I currently don't have access to my code in question. The button needs to be an input submit to go to [HTTPPOST]. However, if you need any more information please let me know.
Thank you for your time,
HtmlNooby
I solved it by wrapping a button with the following. This creates binds each individual button with the given item from an array. Kind of acts like a buttonfor if you will.
Foreach item in array{
#using(Html.BeginForm(…)
{
<button class=input value=item>item</button>
}
}
So far I've created a table from a database query using the input from a dropdown menu. Now I want the user to be able to click on a cell in that table and using the the value of that cell query the database again for further information. Here's what I have:
echo "
<tr>
<td>".$value['Time']."</td>
<td>".$value['First_Name']."</div></td>
...
This continues on for the rest of the information but thats the code for my initial table. The user should then click on First_Name for further information provided by an AJAX request. My jQuery so far looks like this...I haven't even been able to start the AJAX as I can't pass in the value of the table cell.
$(document).ready(function(){
$('#tables').on('click','td', function(){
var module= $(this).val();
alert(module);
})
Once I figure out how to pass this value then I can move ahead with the database query but this is holding me back. Do I even need to do this if Im using JQuery AJAX? Any help is appreciated.
if you want get the text inside the element tag. Please use .text() not .val()
example:
var module= $(this).text();
I hope this usefull :)
It would be better to bind click event on <a> instead of <td>. That will work good too.
Now question is you want to pass some value through AJAX to PHP script. Good practice to do this is by setting data-* attributes to <a> element.
So your HTML/PHP code will look something like this,
<td>".$value['Time']."</td>
<td>".$value['First_Name']."</div></td>
In above code we've given a class to <a> to bind click event on each element having this class. (Useful for multiple records generated using loop), we have also set data-modelvalue attribute to <a>.
Now update your jQuery code like this,
$('#tables').on('click','.myLink', function(e){
e.preventDefault();
var module= $(this).data('modelvalue');
alert(module);
});
In above code we have bind click event on myLink class elements. Also we're fetching data-modelvalue using .data() jQuery method.
e.preventDefault() forces default action of the event to not be triggered, in our case page won't get refreshed(which is default action of any hyperlink).
References:
https://developer.mozilla.org/en/docs/Web/Guide/HTML/Using_data_attributes
https://api.jquery.com/data/
https://api.jquery.com/event.preventdefault/
Ok, so this is a continuation of my How to hide the default value of a form field? question. I've got a form where I sometimes need to change one of the values before submitting it to another website. I used getElementById to do this, and it seemed to have worked, but then when the submission actually goes through the change hasn't been made. Here's the code I used:
function changeAmount() {
document.getElementById("amount").value = 50;
form=document.getElementById('myform');
form.target='_blank';
form.action='http://www.example.com';
form.submit();
}
I also tried taking out the submission code and putting the website in the action for the original form tag. No dice.
So what's stopping the change from going through?
Thanks in advance!
The HTML (in the question you link to) doesn't have any element with the ID amount, it has an element with the ID Amount. Identifiers are case sensitive.
(You have another issue in that you actually have three elements with that ID and IDs must be unique in a document).
I have a dilema which I'm unsure how to approach right now. I know that JQuery needs to have a unique set of ID's to be called in the document ready function. I go through PHP and read my mysql table to print out these HTML forms and with each form is a button that will add a new item to this table.
The issue here is that I cannot have an idea of how many forms there will be so I would like to write the JQuery code so that it can dynamically read anytime that the button is clicked, but know which button was clicked so that the proper ID's can pass.
I've seen some examples but they have more to do with CSS styling, are there any ideas or thoughts as to how this problem could be remedied?
If you are writing out the forms in a for loop with php you can assign each submit button an id using the iterator, like submit_1, submit_2 etc and then you can have an on click handler in jquery using a selector contains, something like:
$(document).on('click', 'input[id*="submit_"]', function() {
//code goes here
alert( $(this).prop('id') );
});
I have a form with button A and button B. It's sent by a jQuery function called clicking on one of the buttons. At the end of this long function which is checking prerequisites, the form is sent using this line:
$('#wlform').submit();
I want to adjust this code to send something to be able to distinguish which button was pressed. Something in JavaScript similar to <input type="submit" name="submitbutton1"/>
Provide us with some code?
I think you're talking about two buttons that both should have their own ID's. You could try and catch the ID attributes after you click them;
$(this).attr('id');
Or change 'id' into 'name' if you want to get that value.
I suppose you use a javascrit click event to execute your javascript functions.
In javascript, you can add a hidden input to your form :
$(...).click(function() {
... // Your code
var clicked_button = $(this);
$('#wlform').append($('<input type="hidden" name="clicked-button"/>').val(clicked_button.attr('id'));
$('#wlform').submit();
});
With that, the id of the clicked_button will be sent with the form.
Just give to the hidden input the value of the button id attribute. You could do something similar to this (before the submit statement):
$('input[type=hidden]').val($(this).attr('id'));
Where $(this) is the button clicked.
None of the answers worked, so I've put something together from these on my own. I've added a hidden input field, clicked-button as you suggested. Then when calling my precheck_submit function, I pass another parameter (c) for storing which has been clicked. In the precheck_submit function I added $('#clicked-button').val(c);. It works. Anyways, thanks for your efforts.