Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Jak w juery za pomocą linków dynamicznie zmienić zawartość value w input?
marcus755
post
Post #1





Grupa: Zarejestrowani
Postów: 158
Pomógł: 1
Dołączył: 6.12.2012

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


Hej wszystkim:-)

Jak zrobić w jquery?
żeby gdy klikamy link o id="test1" - value zmienia się o +1,
a gdy klikamy link o id="test0", to zmniejsza się o -1, a gdy mamy w value=0 (to nie zmniejsza mniej niż 0)

Poniżej skrypt, który napisałem, ale nie działa...

  1.  
  2. <a href="#" id="test0"></a>
  3. <label>Ilość</label>
  4. <input name="0" type="text" value="0" id="test11">
  5. <a href="#" id="test1"></a>
  6.  
  7. var n = 0;
  8. $("#test1").click(function (e) {
  9. e.preventDefault(); // prevent the default action
  10. e.stopPropagation; // stop the click from bubbling
  11. var text = $(this).text();
  12. $("input").find("test11").val(++n);
  13. });
  14.  
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 10)
lukasz1985
post
Post #2





Grupa: Zarejestrowani
Postów: 205
Pomógł: 43
Dołączył: 5.03.2012

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


[JAVASCRIPT] pobierz, plaintext
  1. <a href="#" id="test0"></a>
  2. <label>Ilość</label>
  3. <input name="0" type="text" value="0" id="test11">
  4. <a href="#" id="test1">Wiecej</a>
  5.  
  6. <script>
  7. var n = 0;
  8. $("#test1").click(function (e) {
  9.  
  10. e.preventDefault(); // prevent the default action
  11. e.stopPropagation; // stop the click from bubbling
  12. var text = $(this).text();
  13. $("input#test11").val(++n);
  14. });
  15. </script>
[JAVASCRIPT] pobierz, plaintext
Go to the top of the page
+Quote Post
marcus755
post
Post #3





Grupa: Zarejestrowani
Postów: 158
Pomógł: 1
Dołączył: 6.12.2012

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


Kod
<a href="#" id="test0"></a>
<label>Ilość</label>
<input name="0" type="text" value="0" id="test11">
<a href="#" id="test1"></a>

<script>
var n = 0;
$(document).ready(function(){
  $("#test1").click(function(){
    $("input:text#test11").val(++n);
  });
  $("#test0").click(function(){
    $("('input:text#test11').val(++n);").val(-1);
  });
});
</script>


Działa mi dodawanie.
A jak zrobić, żeby mi odejmował bieżący stan value?
(np. mam w danym momencie w input value=5, klikając w link o id="test0", zmniejsza mi się o 1, aż do wartości wyjściowej value=0)
Go to the top of the page
+Quote Post
nospor
post
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




$("('input:text#test11').val(++n);").val(-1);
(IMG:style_emoticons/default/facepalmxd.gif)

No to skoro chcesz odejmować to odejmuj a nie dodajesz....

$("input:text#test11").val(--n);
Go to the top of the page
+Quote Post
marcus755
post
Post #5





Grupa: Zarejestrowani
Postów: 158
Pomógł: 1
Dołączył: 6.12.2012

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


Thnx:-)
a jak zrobić, że jak dojdzie do 0, to już nie wrzuca liczb ujemnych?
Go to the top of the page
+Quote Post
nospor
post
Post #6





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




No nie wiem.... pomyślmy.... może zastosować banalny IF?
Go to the top of the page
+Quote Post
marcus755
post
Post #7





Grupa: Zarejestrowani
Postów: 158
Pomógł: 1
Dołączył: 6.12.2012

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


coś nie wychodzi...
Go to the top of the page
+Quote Post
nospor
post
Post #8





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Zajebisty opis problemu.... może pokaż co próbujesz zrobić, bo na chwilę obecną to mam wrażenie że masz niesamowitego lenia i liczysz, aż ci to niesamowicie trudne zadanie podadzą na tacy.
Go to the top of the page
+Quote Post
marcus755
post
Post #9





Grupa: Zarejestrowani
Postów: 158
Pomógł: 1
Dołączył: 6.12.2012

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


  1. <a href="#" id="test0"></a>
  2. <label>Ilość</label>
  3. <input name="0" type="text" value="0" id="test11">
  4. <a href="#" id="test1"></a>
  5.  
  6. var n = 0;
  7. $(document).ready(function(){
  8. $("#test1").click(function(){
  9. $("input:text#test11").val(++n);
  10. });
  11. $("#test0").click(function(){
  12. $("input:text#test11").val(--n);
  13.  
  14. if (
  15. $("input:text#test11").val(n=0);
  16. );
  17. return true;
  18.  
  19. });
  20. });


ale nie działa nie wiem, jak zrobić, aby nie wrzucał liczb mniejszych niż 0.

Ten post edytował marcus755 10.06.2013, 14:39:22
Go to the top of the page
+Quote Post
Talidali
post
Post #10





Grupa: Zarejestrowani
Postów: 79
Pomógł: 19
Dołączył: 31.05.2013

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


  1. if (
  2. $("input:text#test11").val(n=0);
  3. );



Widzę, że brak podstaw.

  1. if ($("input:text#test11").val()==0)
Go to the top of the page
+Quote Post
marcus755
post
Post #11





Grupa: Zarejestrowani
Postów: 158
Pomógł: 1
Dołączył: 6.12.2012

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


pokombinowałem i działa:-)
thnx:-)

Ten post edytował marcus755 10.06.2013, 14:59:11
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 24.08.2025 - 00:41