Jump to content

Recommended Posts

Hello.

 

I have this simple html code

 

<select name="ColumnsNumberDropdown">
         <option value="one">Select</option>
         <option value="one">1</option>
         <option value="two">2</option>
         <option value="three">3</option>
         <option value="four">4</option>
         <option value="five">5</option>
         <option value="six">6</option>
         <option value="seven">7</option>
         </select><br><hr>
         <label for="column_name" style="padding-right:10px;">Column1 name:</label>
         <input type="text" name="ColumnNameInput" disabled="true" /><br>
         <label for="column_name" style="padding-right:10px;">Column2 name:</label>
         <input type="text" name="Column2NameInput"  disabled="true"/><br>
         <label for="column_name" style="padding-right:10px;">Column3 name:</label>
         <input type="text" name="Column3NameInput"  disabled="true"/><br>
         <label for="column_name" style="padding-right:10px;">Column4 name:</label>
         <input type="text" name="Column4NameInput"  disabled="true"/><br>
         <label for="column_name" style="padding-right:10px;">Column5 name:</label>
         <input type="text" name="Column5NameInput"  disabled="true"/><br>
         <label for="column_name" style="padding-right:10px;">Column6 name:</label>
         <input type="text" name="Column6NameInput"  disabled="true"/><br>
         <label for="column_name" style="padding-right:10px;">Column7 name:</label>
         <input type="text" name="Column7NameInput"  disabled="true"/>

 

As you can see all <input> are disabled. When user chooses from the select menu e.g the number 2 i want "ColumnNameInput" and "Column2NameInput" inputs to be enabled. If someone choose number 5 from menu , first 5 inputs to be enabled and so on...

 

I've tried this but i need to make it more specific on what user chosen and not because the select item changed.

 

$("select[name='ColumnsNumberDropdown']").change(function () {
    
    var text = $("input[name='ColumnNameInput']");
    
    if (text.attr('disabled')) {
      text.removeAttr('disabled');
    }
    else
    {
      text.attr('disabled', 'disabled');
    }
});

 

Any help will be thankful.

Link to comment
https://forums.phpfreaks.com/topic/267988-enable-text-inputs-form-a-select-menu/
Share on other sites

Here's a little example of what I think you're after.

http://xaotique.no-ip.org/tmp/4.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>Temporary Page</title>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>

	<style type="text/css">
		#inputs
		{
			float: left;
			width: 250px;
		}

		#inputs input
		{
			width: 240px;
		}
	</style>

	<script type="text/javascript">
		$(document).ready(function()
		{
			function disableInputs()
			{
				$("#inputs input").each(function()
				{
					$(this).attr("disabled", "disabled");
				});
			}

			disableInputs();

			$("#selection option").click(function(e)
			{
				var select = parseInt($(this).html()) - 1;

				disableInputs();
				$("#inputs input:eq(" + select + ")").removeAttr("disabled");
			});
		});
	</script>
</head>

<body>
	<div id="inputs">
		<input type="text" />
		<input type="text" />
		<input type="text" />
		<input type="text" />
		<input type="text" />
	</div>

	<select id="selection">
		<option value="one">1</option>
		<option value="two">2</option>
		<option value="three">3</option>
		<option value="four">4</option>
		<option value="five">5</option>
	</select>
</body>
</html>

Page updated and here's the new code.  It's not as nice, but it works on Chrome too ..

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>Temporary Page</title>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>

	<style type="text/css">
		#inputs
		{
			float: left;
			width: 250px;
		}

		#inputs input
		{
			width: 240px;
		}
	</style>

	<script type="text/javascript">
		$(document).ready(function()
		{
			function disableInputs()
			{
				$("#inputs input").each(function()
				{
					$(this).attr("disabled", "disabled");
				});
			}

			disableInputs();

			$("#selection").click(function(e)
			{
				disableInputs();
				var selected = $(this).val(), select = 0;

				$("#selection option").each(function()
				{
					if ($(this).val() == selected)
						select = parseInt($(this).html()) - 1;
				});

				$("#inputs input:eq(" + select + ")").removeAttr("disabled");
			});
		});
	</script>
</head>

<body>
	<div id="inputs">
		<input type="text" />
		<input type="text" />
		<input type="text" />
		<input type="text" />
		<input type="text" />
	</div>

	<select id="selection">
		<option value="one">1</option>
		<option value="two">2</option>
		<option value="three">3</option>
		<option value="four">4</option>
		<option value="five">5</option>
	</select>
</body>
</html>

As a quick thought, I'd put a default selection "Select Input" or whatever .. then use the index of the each loop.  Little cleaner, won't need parsed, and won't trigger when you're selecting the first input.  The default could also be to disable all of them, depending on what you're actually doin' with it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.