Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [HTML][JavaScript]select i akcja na input
robos85
post 17.09.2008, 10:17:42
Post #1





Grupa: Zarejestrowani
Postów: 466
Pomógł: 11
Dołączył: 21.09.2006
Skąd: Szczecin

Ostrzeżenie: (0%)
-----


Mam taki kod JS:
  1. window.onload=Load;
  2.  
  3. function Load(){
  4. document.forms['compose_form'].reciever.enabled=true;
  5. }
  6. function check_reciever(){
  7. var form_compose=document.getElementById('friends_list');
  8. var selIndex=form_compose.selectedIndex;
  9. if form_compose.options[selIndex].value!=0{
  10. document.forms['compose_form'].reciever.disabled=true;
  11. }
  12. else{
  13. document.forms['compose_form'].reciever.enabled=true;
  14. }
  15. }

i form :
  1. <form action="/inbox" method="post" name="compose_form">
  2. <div style="width:565px;">
  3. <div style="float:left;width:52px;min-width:52px;text-align:left;padding-top:5px;">
  4. Do:
  5. </div>
  6. <div style="float:left;width:300px;">
  7. <input type="text" name="reciever" style="width:295px;" />
  8. </div>
  9. <div style="float:left;text-align:right;width:213px;">
  10. <select style="width:180px;" id="friends_list" onchange="check_reciever()">
  11. <option value="0">----------</option>
  12. <option value="1">11111111</option>
  13. <option value="2">22222222</option>
  14. <option value="3">33333333</option>
  15. </select>
  16. </div>
  17. </div>
  18. <div style="width:565px;clear:both;padding-top:10px;">
  19. <div style="float:left;width:52px;text-align:left;padding-top:5px;">Temat:</div>
  20. <div style="float:left;width:513px;"><input type="text" name="reciever" style="width:513px;" /></div>
  21. </div>
  22. <div style="width:565px;clear:both;padding-top:10px;">
  23. <div style="float:left;width:52px;text-align:left;">
  24. Treść:
  25. </div>
  26. <div style="float:left;width:513px;">
  27. <textarea style="width:513px;height:200px;"></textarea>
  28. </div>
  29. </div>
  30. <div style="width:565px;clear:both;padding-top:10px;text-align:right;">
  31. <button style="width:96px;height:24px;border:none;background:url(../images/send_button.png) no-repeat;"></button>
  32. </div>
  33. </form>


I gdy wybieram jakąś opcję z selecta, pole input nie przyjmuje atrybutu "disabled".
Co skopałem?
Go to the top of the page
+Quote Post
UNK
post 17.09.2008, 10:25:21
Post #2





Grupa: Zarejestrowani
Postów: 93
Pomógł: 5
Dołączył: 30.04.2006
Skąd: Gliwice

Ostrzeżenie: (0%)
-----


Kod
document.forms['compose_form'].reciever.enabled=true;


nie powinno być

Kod
document.forms['compose_form'].reciever.disabled=false;


?


EDIT @DOWN:

Kod
var form_compose=document.getElementById('friends_list');
var selIndex=form_compose.selectedIndex;

Czapla, raczej nie, aczkolwiek to tu może siedzieć błąd, ponieważ selIndex nie dostaje wartości pomimo, że form_compose.selectedIndex jest dobrze pobierane.
robos85 coś jest nie tak w tych 2 liniach tongue.gif

Ten post edytował UNK 17.09.2008, 10:36:44
Go to the top of the page
+Quote Post
robos85
post 17.09.2008, 10:26:07
Post #3





Grupa: Zarejestrowani
Postów: 466
Pomógł: 11
Dołączył: 21.09.2006
Skąd: Szczecin

Ostrzeżenie: (0%)
-----


nie, chodzi mi głównie o to, że nie działa nic nawet po zmianie
Go to the top of the page
+Quote Post
Czapla
post 17.09.2008, 10:28:24
Post #4





Grupa: Zarejestrowani
Postów: 130
Pomógł: 3
Dołączył: 10.12.2007
Skąd: Bielsko-Biała

Ostrzeżenie: (0%)
-----


a nie powinno być

  1. var selIndex=compose_form.selectedIndex;
?


--------------------
Life is a game, continue playing and try to be a pr0.
Go to the top of the page
+Quote Post
robos85
post 17.09.2008, 10:33:55
Post #5





Grupa: Zarejestrowani
Postów: 466
Pomógł: 11
Dołączył: 21.09.2006
Skąd: Szczecin

Ostrzeżenie: (0%)
-----


Cytat(Czapla @ 17.09.2008, 11:28:24 ) *
a nie powinno być

  1. var selIndex=compose_form.selectedIndex;
?



form_compose to zmienna zadeklarowana linijkę wyżej:/

Ten post edytował robos85 17.09.2008, 10:34:26
Go to the top of the page
+Quote Post
nevt
post 17.09.2008, 10:36:21
Post #6





Grupa: Przyjaciele php.pl
Postów: 1 595
Pomógł: 282
Dołączył: 24.09.2007
Skąd: Reda, Pomorskie.

Ostrzeżenie: (0%)
-----


nie ma osobno właściwości enabled i disabled - jest tylko disabled


--------------------

-
Oh no, my young coder. You will find that it is you who are mistaken, about a great many things... -
Go to the top of the page
+Quote Post
robos85
post 17.09.2008, 10:38:37
Post #7





Grupa: Zarejestrowani
Postów: 466
Pomógł: 11
Dołączył: 21.09.2006
Skąd: Szczecin

Ostrzeżenie: (0%)
-----


Kod
window.onload=Loadform;

function Loadform(){
document.forms['compose_form'].reciever.disabled=true;
}
function check_reciever(){
var form_compose=document.getElementById('friends_list');
var selIndex=form_compose.selectedIndex;
if form_compose.options[selIndex].value!=0{
document.forms['compose_form'].reciever.disabled=true;
}
else{
document.forms['compose_form'].reciever.disabled=false;
}
}


Zadeklarowałem na początku dla pola reciever żeby było od razu disabled, lecz nawet to nie działa:/
Go to the top of the page
+Quote Post
UNK
post 17.09.2008, 10:43:47
Post #8





Grupa: Zarejestrowani
Postów: 93
Pomógł: 5
Dołączył: 30.04.2006
Skąd: Gliwice

Ostrzeżenie: (0%)
-----


Napisałem wyżej w moim poście, ale chyba już nikt tego nie zauważy tongue.gif
var selIndex=form_compose.selectedIndex;
selIndex nie dostaje wartości jest undefined przez co reszta skryptu się nie wykonuje.
Po małej zamianie i wstawieniu tego bezpośrednio do ifa
form_compose.options[form_compose.selectedIndex].value!=0
wywala: 'form_compose.options is undefined'
Coś nie tak jest z tymi kilkoma liniami tongue.gif
Go to the top of the page
+Quote Post
drPayton
post 17.09.2008, 10:45:20
Post #9





Grupa: Zarejestrowani
Postów: 890
Pomógł: 65
Dołączył: 13.11.2005
Skąd: Olsztyn

Ostrzeżenie: (0%)
-----


@nevt napisał dość wyraźnie, ale powtórzę:
  1. <input type="text" id="bleh">
  2.  
  3. <script type="text/javascript">
  4. document.getElementById('bleh').disabled = "disabled";


Widzisz różnicę? snitch.gif
Go to the top of the page
+Quote Post
robos85
post 17.09.2008, 10:49:29
Post #10





Grupa: Zarejestrowani
Postów: 466
Pomógł: 11
Dołączył: 21.09.2006
Skąd: Szczecin

Ostrzeżenie: (0%)
-----


Cytat(drPayton @ 17.09.2008, 11:45:20 ) *
@nevt napisał dość wyraźnie, ale powtórzę:
  1. <input type="text" id="bleh">
  2.  
  3. <script type="text/javascript">
  4. document.getElementById('bleh').disabled = "disabled";


Widzisz różnicę? snitch.gif


tak dlatego pozmieniałem na disabled=true lub disabled=false

Ten post edytował robos85 17.09.2008, 10:50:05
Go to the top of the page
+Quote Post
drPayton
post 17.09.2008, 11:04:20
Post #11





Grupa: Zarejestrowani
Postów: 890
Pomógł: 65
Dołączył: 13.11.2005
Skąd: Olsztyn

Ostrzeżenie: (0%)
-----


  1. function check_reciever(){
  2. var form_compose=document.getElementById('friends_list');
  3. var selIndex=form_compose.selectedIndex;
  4. if (selIndex != 0) {
  5. document.forms['compose_form']['reciever'][0].disabled="disabled";
  6. } else {
  7. document.forms['compose_form']['reciever'][0].disabled="";
  8. }
  9. }


Bo masz dwa pola o nazwie 'receiver'...
Go to the top of the page
+Quote Post
robos85
post 17.09.2008, 11:04:38
Post #12





Grupa: Zarejestrowani
Postów: 466
Pomógł: 11
Dołączył: 21.09.2006
Skąd: Szczecin

Ostrzeżenie: (0%)
-----


EDIT:

zmianiam cały js kod na:
Kod
window.onload=Loadformcompose;

function Loadformcompose(){
    document.forms['compose_form'].reciever.disabled=true;
}
i to nie działa, ale jak dam:
Kod
window.onload=Loadformcompose;

function Loadformcompose(){
    var f_reciever=document.getElementById('reciever');
    f_reciever.disabled=true;
}
to działa.

Dlaczego jak odwołuję się do pól formularza poprzez name to nie działa, a jak getElementById robię to działa?:/
Go to the top of the page
+Quote Post
drPayton
post 17.09.2008, 11:06:39
Post #13





Grupa: Zarejestrowani
Postów: 890
Pomógł: 65
Dołączył: 13.11.2005
Skąd: Olsztyn

Ostrzeżenie: (0%)
-----


Wyżej w moim poście masz odpowiedź winksmiley.jpg
Go to the top of the page
+Quote Post
robos85
post 17.09.2008, 11:10:07
Post #14





Grupa: Zarejestrowani
Postów: 466
Pomógł: 11
Dołączył: 21.09.2006
Skąd: Szczecin

Ostrzeżenie: (0%)
-----


Śmiga smile.gif

Tylko czemu nie mogę się odnościć poprze .reciever a muszę poprzez ['reciever']questionmark.gif

Ten post edytował robos85 17.09.2008, 11:11:39
Go to the top of the page
+Quote Post
drPayton
post 17.09.2008, 11:13:35
Post #15





Grupa: Zarejestrowani
Postów: 890
Pomógł: 65
Dołączył: 13.11.2005
Skąd: Olsztyn

Ostrzeżenie: (0%)
-----


Jeśli masz tylko jedno pole o nazwie 'receiver' to możesz (...).receiver.(...), jeśli więcej, to już jest tablica i tylko tak jak napisałem smile.gif
Go to the top of the page
+Quote Post
Shili
post 17.09.2008, 11:22:01
Post #16





Grupa: Zarejestrowani
Postów: 1 085
Pomógł: 231
Dołączył: 12.05.2008

Ostrzeżenie: (0%)
-----


Jednak moim zdaniem lepiej jest skorzystać z setAttribute i removeAttribute zamiast disabled="". To tak na marginesie.
Go to the top of the page
+Quote Post
drPayton
post 17.09.2008, 11:34:21
Post #17





Grupa: Zarejestrowani
Postów: 890
Pomógł: 65
Dołączył: 13.11.2005
Skąd: Olsztyn

Ostrzeżenie: (0%)
-----


@Shili: Uzasadnij proszę. Z chęcią dowiem się czegoś nowego (nie, tu nie ma złośliwości, poważnie pytam smile.gif

@down: Aaaa, myślałem, że to co naskrobałem nie działa w jakiś przypadkach winksmiley.jpg No, niewątpliwie lepiej wygląda Twoje podejście smile.gif

Ten post edytował drPayton 17.09.2008, 11:47:28
Go to the top of the page
+Quote Post
Shili
post 17.09.2008, 11:41:38
Post #18





Grupa: Zarejestrowani
Postów: 1 085
Pomógł: 231
Dołączył: 12.05.2008

Ostrzeżenie: (0%)
-----


Bardziej eleganckie. I profesjonalne. Skoro atrybut nie jest potrzebny nie ma sensu zostawiać pustej wartości, skoro bez żadnego problemu można ten atrybut usunąć, a później go przywrócić w razie potrzeby. W kodzie nie zostają żadne śmieci.
Oczywiście - ustawianie atrybutu jako pusty nie jest niepoprawnością ze względu na strukturę dokumentu.

W tym momencie funkcja check_reciever() wyglądałaby jakoś tak:
Kod
function check_reciever(){
    var form_compose=document.getElementById('friends_list');
    var selIndex=form_compose.selectedIndex;
    if (selIndex != 0) {
        document.forms['compose_form']['reciever'][0].setAttribute('disabled', 'disabled');
    } else {
        document.forms['compose_form']['reciever'][0].removeAttribute('disabled');
    }
}


Ten post edytował Shili 17.09.2008, 11:42:06
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 18.07.2025 - 01:39